package com.peanut.devlibrary;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.peanut.baby.R;
import com.peanut.devlibrary.util.AnimUtil;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.util.SysUtil;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by anonymouslystudio on 2017/6/5.
 */

public class BaseActivity extends AppCompatActivity {
    protected AlertDialog mProgressDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void showToast(final String s){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * show progress dialog
     *
     * @param msg 提示信息
     */
    public void showProgressDialog(final String msg, final boolean cancelable) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isFinishing())
                    return;
                dismissProgressDialog();
                AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
                mProgressDialog = builder.create();
                mProgressDialog.setCancelable(cancelable);
                mProgressDialog.show();
                final Window window = mProgressDialog.getWindow();
                window.setContentView(R.layout.library_dlg_progress);

                WindowManager.LayoutParams params = window.getAttributes();
                params.gravity = Gravity.CENTER;
                params.width = SysUtil.dip2px(150);
                params.height = SysUtil.dip2px(150);
                window.setAttributes(params);


                ImageView image = (ImageView) window.findViewById(R.id.progress_img);
                image.startAnimation(AnimUtil.getLoadingAnimation());
                TextView content = (TextView) window.findViewById(R.id.progress_msg);
                if (!StringUtil.isNullOrEmpty(msg)) {
                    content.setText(msg);
                }

            }
        });

    }

    /**
     * @param msg
     * @param cancelable
     */
    public void updateProgressDialog(final String msg, final boolean cancelable) {
        dismissProgressDialog();
        showProgressDialog(msg,cancelable);

    }

    /**
     * dismiss progress
     */
    public void dismissProgressDialog() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (mProgressDialog != null) {
                        if (mProgressDialog.isShowing()) {
                            mProgressDialog.dismiss();
                        }
                        mProgressDialog = null;
                    }
                } catch (Exception e) {

                }
            }
        });

    }

    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyboard() {
        // 隐藏软键盘
        try {
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(this.getCurrentFocus()
                                    .getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
        }
    }

}
